{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<link href="{__STATIC__}/home/other/formSelects-v4.css" rel="stylesheet"/>
<script src="{__STATIC__}/home/other/jquery.min.js"></script>
<script src="{__STATIC__}/home/other/formSelects-v4.min.js"></script>
<style>
    .layui-laydate-header {
        display: none;
    }

    .layui-laydate-content > table > thead {
        display: none;
    }

    .laydate-day-prev {
        display: none;
    }

    .laydate-day-next {
        display: none;
    }

    .pl_btn {
        color: #4c9cfb !important;
        cursor: pointer;
    }

    .countBan {
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        align-content: center;
        justify-content: flex-end;
    }

    .count_item_show {
        display: flex;
        margin: 10px;
        align-content: center;
        align-items: center;
    }
</style>
<form class="layui-form p-4">
    <h3 class="pb-3">功能菜单/节点</h3>

    <table class="layui-table layui-table-form">
        <tr>
            <td class="layui-td-gray-2">销售合同号<font>*</font>
            </td>
            <td>
                <!--                <input type="text" name="khHtNum" value="{$detail.khHtNum|default=''}" class="layui-input" lay-verify="required" placeholder="请输入">-->
                <input type="text" name="orderNum" value="{$detail.orderNum|default=''}" lay-verify="required" readonly
                       autocomplete="off" placeholder="请输入"
                       class="layui-input disabled">
            </td>
            <td class="layui-td-gray">销售单位<font>*</font>
            </td>
            <td>
                <select name="salesUnitId" lay-search lay-filter="salesUnitId">
                    <option value="">销售单位</option>
                    {volist name='saleUniList' id='sl'}
                    <option value="{$sl.id}" {if isset($detail.salesUnitId)&&$detail.salesUnitId==$sl.id} selected {/if}>{$sl.title}</option>
                    {/volist}
                </select>
            </td>

            <td class="layui-td-gray">单据日期<font>*</font></td>
            <td>
                <input type="text" name="orderDate" id="orderDate" value="{$detail.orderDate|default=$thisDate}"
                       lay-verify="required" readonly
                       autocomplete="off" placeholder="选择日期"
                       class="layui-input">
            </td>
        </tr>
        <tr>
            <td class="layui-td-gray">客户名称<font>*</font>
            </td>
            <td>
                <select name="punterId" id="punterId" lay-search lay-filter="punterId">
                    <option value="">选择客户</option>
                    {volist name='punterList' id='pl'}
                    <option value="{$pl.id}" {if isset($detail.punterId)&&$detail.punterId==$pl.id} selected {/if}>{$pl.name}</option>
                    {/volist}
                </select>
            </td>
            <td class="layui-td-gray-2">销售员<font>*</font>
            </td>
            <td>
                <select name="saleId" id="saleId" xm-select="saleId">
                    <option value="">选择销售员</option>
                    {volist name='saleUserList' id='sul'}
                    <option value="{$sul.id}" {if isset($detail.saleId)&&in_array($sul.id,explode(',',$detail.saleId))} selected {/if}>{$sul.name}</option>
                    {/volist}
                </select>
            </td>
            <td class="layui-td-gray">付款方式<font>*</font>
            </td>
            <td>
                <select name="payType" lay-search>
                    {volist name='payTypeList' id='ptl'}
                    <option value="{$ptl.id}" {if isset($detail.payType) && $detail.payType== $ptl.id} selected {/if}>{$ptl.name}</option>
                    {/volist}
                </select>
            </td>
        </tr>
        <tr>
            <td class="layui-td-gray">联系人
            </td>
            <td>
                <select name="contactId" id="contactId" lay-filter="contactId" lay-search>
                    <option value="">供应商联系人</option>
                </select>
            </td>

            <td class="layui-td-gray">收货人
            </td>
            <td>
                <select name="saveId" id="saveId" lay-search lay-filter="saveId">
                    <option value="">请选择</option>
                </select>
            </td>
            <td class="layui-td-gray-2">税率(%)
            </td>
            <td>
                <select name="tax" id="tax">

                    {if isset($detail.tax)}
                    {volist name='quoOrderTax' id='qt'}
                    <option value="{$qt}" {if $detail.tax==$qt} selected{/if}>{$qt}</option>
                    {/volist}
                    {else}
                    {volist name='quoOrderTax' id='qt'}
                    <option value="{$qt}" {if $qt==13} selected{/if}>{$qt}</option>
                    {/volist}
                    {/if}

                </select>
                <!--                <input type="text" name="tax" id="tax" value="{$detail.tax|default=''}"-->
                <!--                       oninput="digitalFiltering(this,4)" max="1" autocomplete="off" placeholder="请输入"-->
                <!--                       lay-reqText="请输入" class="layui-input">-->
            </td>
        </tr>

        <tr>
            <td class="layui-td-gray-2">客户合同号
            </td>
            <td>
                <input type="text" name="khHtNum" value="{$detail.khHtNum|default=''}" class="layui-input"
                       placeholder="请输入">
            </td>
            <td class="layui-td-gray-2">关联项目</td>
            <td>
                <table>
                    <tr>
                        <td><select name="projectId" id="selectProName" lay-search lay-filter="selectProName">
                            <option value="">请选择</option>
                            {volist name="projectList" id="pl"}
                            <option value="{$pl.id}" {if isset($detail.projectId) && $detail.projectId== $pl.id}
                                    selected{/if}>{$pl.name}</option>
                            {/volist}
                        </select></td>
                        <td><input type="text" name="glProjectName" class="layui-input"
                                   value="{$detail.glProjectName|default=''}">
                        </td>
                    </tr>
                </table>

            </td>
            <td class="layui-td-gray">打印模板</td>
            <td>
                <select name="printTemplate">
                    {volist name='printTemplateList' id='ptl'}
                    <option value="{$ptl.id}" {if isset($detai.printTemplate)&&$detail.printTemplate==$ptl.id} selected
                            {/if}>{$ptl.name}</option>
                    {/volist}
                </select>
            </td>
        </tr>

        <tr>
            <td class="layui-td-gray">优惠金额</td>
            <td>
                <input type="text" readonly name="countDiscount" class="layui-input countDiscount" value="{$detail.countDiscount|default=0}" placeholder="优惠金额">
            </td>
            <td class="layui-td-gray-2">客户订单号
            </td>
            <td>
                <input type="text" name="khOrderNum" value="{$detail.khOrderNum|default=''}"
                       class="layui-input " placeholder="请输入">
            </td>
            <td class="layui-td-gray-2">合同备注
            </td>
            <td>
                <input type="text" name="htRemark" value="{$detail.htRemark|default=''}"
                       class="layui-input " placeholder="请输入">
            </td>
        </tr>
        <tr style="display: none">

            <td class="layui-td-gray-2">联系电话
            </td>
            <td>
                <input type="text" name="contactPhone" value="{$detail.contactPhone|default=''}" readonly
                       class="layui-input layui-disabled" placeholder="选择联系人">
                <input type="hidden" name="contactName" value="{$detail.contactName|default=''}">
            </td>

        </tr>
        <tr style="display: none">


            <td class="layui-td-gray-2">收货人电话

            </td>
            <td><input type="text" name="savePhone" value="{$detail.savePhone|default=''}" readonly
                       class="layui-input layui-disabled" placeholder="选择收货人">
                <input type="hidden" name="saveName" value="{$detail.saveName|default=''}">
            </td>
            <td class="layui-td-gray-2">收货人地址
            </td>
            <td>
                <input type="text" name="saveAdd" value="{$detail.saveAdd|default=''}" readonly
                       class="layui-input layui-disabled" placeholder="选择收货人">

            </td>
        </tr>
        <tr>
            <td colspan="6"><h4>明细</h4></td>
        </tr>
        <tr>

            <td colspan="6">
                {if !isset($detail.status)||$detail.status==1}
                <a href="javascript:" data-url="{:url('addMx')}" onclick="addPro()" data-width="1000"
                   class="layui-btn layui-btn-primary ">选择产品</a>
                <a href="javascript:" data-url="{:url('addMx')}" data-width="1000"
                   class="layui-btn layui-btn-primary " onclick="getBjd()">关联报价单</a>
                <a href="{:url('api/ExportModel/SaleContractMxImportModel')}" target="_blank"
                   class="layui-btn layui-btn-normal">导入模板</a>
                <a href="javascript:" class="layui-btn layui-btn-primary " id="importMx">导入</a>
                {/if}
                <table class="layui-table tableMany">
                    <thead>
                    <tr>
                        <td>序号</td>
                        <!--                        <td>物料编码</td>-->
                        <td>品牌</td>
                        <td>品名</td>
                        <td>型号</td>
                        <td>订货号</td>
                        <td>单位</td>
                        <td>数量</td>
                        <td>含税单价</td>
                        <td>未税单价</td>
                        <td>含税小计</td>
                        <td>未税总额</td>
                        <td>税额</td>
                        <td>优惠金额</td>
                        <td>货期
                            <a href="javascript:" class="pl_btn" id="mxPl">批量</a>
                        </td>
                        <td>客户料号</td>
                        <td>项目编码</td>
                        <td>备注</td>
                        <!--                        <td>关联报价单</td>-->
                        <td>操作</td>
                    </tr>
                    </thead>
                    <tbody id="mxTbdy">
                    {if $mxList}
                    {volist name='mxList' id='mxl' key='mkey'}
                    <tr>
                        <td>
                            <span class="xuhao">{$mkey}</span>
                            <input type="hidden" name="mx[mxid][]" value="{$mxl.id}">
                            <input type="hidden" name="mx[mdId][]" value="{$mxl.mdId}">
                            <input type="hidden" name="mx[quotationDetailId][]" value="{$mxl.quotationDetailId}">
                            <input type="hidden" name="mx[quotationOderNum][]" class="layui-input"
                                   value="{$mxl.quotationOderNum}" readonly>
                        </td>

                        <td>{$mxl.brand}</td>
                        <td>{$mxl.mdName}</td>
                        <td>{$mxl.specs}</td>
                        <td>{$mxl.buyCode}</td>
                        <td>{$mxl.unit}</td>
                        <td><input type="text" name="mx[bjNum][]" value="{$mxl.bjNum}" class="layui-input bjNum"
                                   onblur="getMoneys(this)"
                                   lay-verify="required" oninput="digitalFiltering(this,4)"></td>
                        <td><input type="text" name="mx[bjPrice][]" value="{$mxl.bjPrice}" class="layui-input bjPrice"
                                   onblur="getMoneys(this)"
                                   lay-verify="required" oninput="digitalFiltering(this,4)"></td>
                        <td><input type="text" class="layui-input noTaxPrice" value="{$mxl.noTaxPrice}" readonly
                                   name="mx[noTaxPrice][]"></td>
                        <td><input type="text" class="layui-input bjMoney" value="{$mxl.bjMoney}" readonly
                                   name="mx[bjMoney][]"></td>
                        <td><input type="text" class="layui-input noTaxMoney" value="{$mxl.noTaxMoney}" readonly
                                   name="mx[noTaxMoney][]"></td>
                        <td><input type="text" class="layui-input taxMoney" value="{$mxl.taxMoney}" readonly
                                   name="mx[taxMoney][]"></td>
                        <td><input type="text" class="layui-input disAmount" value="{$mxl.disAmount}"
                                   name="mx[disAmount][]" oninput="digitalFiltering(this,4)" onblur="countDisCount(this)"></td>

                        <td><input type="text" name="mx[bjTime][]" class="layui-input mxBjTime"
                                   value="{$mxl.bjTime}" placeholder=""></td>
                        <td><input type="text" class="layui-input" name="mx[khlh][]" value="{$mxl.khlh}"></td>
                        <td><input type="text" class="layui-input" name="mx[proCode][]" value="{$mxl.proCode}"></td>
                        <td><input type="text" class="layui-input" name="mx[remark][]" value="{$mxl.remark}"></td>
                        <td>
                            {if !isset($detail.status)||$detail.status==1}
                            <a href="javascript:" class="layui-btn layui-btn-xs layui-btn-danger"
                               onclick="delMx('{$mxl.id}',this)">删除</a>
                            {/if}
                        </td>
                    </tr>
                    {/volist}
                    {/if}
                    </tbody>
                </table>

            </td>
        </tr>
        <tr>
            <td colspan="17">
                <div class="countBan">
                    <div class="count_item_show">数量合计：<span id="numCount">{$countNum|default=0}</span></div>
                    <div class="count_item_show">金额合计：<span id="priceCount">{$countPrice|default=0}</span></div>
                </div>
            </td>
        </tr>
        <tr>

            <td class="layui-td-gray">附件
            </td>
            <td>
                <div style="display: flex">
                    <input type="hidden" name="fjs" id="fjs" value="{$detail.fjs|default=''}"
                           autocomplete="off" placeholder="请输入"
                           class="layui-input">
                </div>

            </td>

        </tr>
    </table>
    {if !$id||(isset($detail.status)&&$detail.status==1)}
    <table class="layui-table layui-table-form" style="margin-top:12px">
        <tr>
            <td class="layui-td-gray">审批流程<font>*</font></td>
            <td colspan="5">
                <select name="flow_id"  lay-filter="flowtype" lay-reqText="请选择审批流程">
                    <option value="">--请选择--</option>
                    {volist name="flows" id="vo"}
                    <option value="{$vo.id}" title="{$vo.check_type}">{$vo.name}</option>
                    {/volist}
                </select>
            </td>
        </tr>
        <tr id="flow_tr">
            <td class="layui-td-gray">审核人<font>*</font></td>
            <td colspan="5">
                <input type="hidden" name="check_admin_ids" value="" readonly><input type="text" name="check_admin_name"
                                                                                     value="" autocomplete="off"
                                                                                     placeholder="请选择审核人"
                                                                                     lay-reqText="请选择审核人"
                                                                                     class="layui-input" readonly>
            </td>
        </tr>
        <tr>
            <td class="layui-td-gray">抄送人</td>
            <td colspan="5">
                <input type="text" name="copy_names" value="" autocomplete="off" placeholder="请选择审核人"
                       class="layui-input picker-more" readonly><input type="hidden" name="copy_uids" value="" readonly>
            </td>
        </tr>
    </table>
    {else}
    <table class="layui-table layui-table-form mt-4">
        <tr>
            <td class="layui-td-gray-2">审核状态</td>
            <td>{eq name="$detail.check_status" value="0"}<span class="black">待审批</span>{/eq}
                {eq name="$detail.check_status" value="1"}<span class="blue">审批中</span>{/eq}
                {eq name="$detail.check_status" value="2"}<span class="green">已通过</span>{/eq}
                {eq name="$detail.check_status" value="3"}<span class="red">已拒绝</span>{/eq}
                {eq name="$detail.check_status" value="4"}<span class="red">已撤销</span>{/eq}
                {eq name="$detail.check_status" value="5"}<span class="yellow">已通过，并打款</span>{/eq}
            </td>
            <td class="layui-td-gray-2">当前审核人</td>
            <td>{$detail.check_user}</td>
            <td class="layui-td-gray">抄送人</td>
            <td>{$detail.copy_user}</td>
        </tr>
        <tr>
            <td class="layui-td-gray">审批流程</td>
            <td colspan="5">
                <div class="flow-flexbox check-items flow-flex-row" id="flowList">
                    <div class="flow-flexbox check-item flow-flex-row">
                        <i class="layui-icon layui-icon-add-circle" data-ok=""></i>
                        <div class="check-item-name">{$detail.create_user}</div>
                        <div class="check-item-status">创建</div>
                        <span class="layui-icon layui-icon-right"></span>
                    </div>
                </div>
            </td>
        </tr>
        {notempty name="$check_record"}
        <tr>
            <td class="layui-td-gray">审批记录</td>
            <td colspan="5">
                <ul class="layui-timeline flow-record pt-2">
                    {volist name="$check_record" id="vo"}
                    <li class="layui-timeline-item delete-{$vo.delete_time}">
                        <i class="layui-icon layui-timeline-axis">&#xe63f;</i>
                        <p style="padding-left:24px">{$vo.check_time_str}<span class="black ml-2">{$vo.name}</span><span
                                class="mx-2 layui-color-{$vo.status}">{$vo.status_str}</span>了此申请。操作意见：<span
                                class="green">{$vo.content}</span></p>
                    </li>
                    {/volist}
                </ul>
            </td>
        </tr>
        {/notempty}
        {if ( $detail.check_status eq 1) AND ( $is_check_admin eq 1) }
        {eq name="$flows.flow_type" value="0"}
        <tr>
            <td class="layui-td-gray">审批节点 <font>*</font></td>
            <td colspan="5">
                <input type="radio" name="check_node" lay-filter="check_node" value="1" title="审核结束">
                <input type="radio" name="check_node" lay-filter="check_node" value="2" title="下一审批人">
                <div class="layui-inline">
                    <input type="text" name="check_admin_name" value="" autocomplete="off" placeholder="请选择下一审批人"
                           lay-verify="required" lay-reqText="请选择下一审批人" class="layui-input picker-one"><input
                        type="hidden" name="check_admin_ids" value="">
                </div>
            </td>
        </tr>
        {/eq}
        <tr>
            <td class="layui-td-gray">审批意见 <font>*</font></td>
            <td colspan="5">
                <textarea name="content" placeholder="请输入审批意见" class="layui-textarea"></textarea>
            </td>
        </tr>
        {/if}
    </table>
    <input type="hidden" name="id" value="{$detail.id}">
    <input type="hidden" name="flow_type" value="{$flows.flow_type}">
    <input type="hidden" name="check_step_sort" value="{$detail.check_step_sort}">
    <input type="hidden" name="check_status" value="{$detail.check_status}">
    <div id="formBtn" style="padding: 10px 0">
        {eq name="$detail.check_status" value='1'}
        {eq name="$is_check_admin" value='1'}
        <span class="layui-btn layui-btn-normal" data-status="1"><i class="layui-icon layui-icon-ok"></i> 通过</span>
        <span class="layui-btn layui-btn-danger" data-status="2"><i class="layui-icon layui-icon-close"></i> {$flows.flow_type == 5?'回退':'拒绝'}</span>
        {/eq}
        {eq name="$is_create_admin" value='1'}
        <!--        <span class="layui-btn layui-btn-primary" data-status="3"><i class="layui-icon layui-icon-reduce-circle"></i> 撤回</span>-->
        {/eq}
        {/eq}
        {if ( $detail.check_status eq 3) AND ( $is_create_admin eq 1) }
        <!--        <span class="layui-btn layui-btn-primary" data-status="3"><i class="layui-icon layui-icon-reduce-circle"></i> 撤回</span>-->
        {/if}
        {if ($detail.check_status==2||$detail.check_status==3)}
        <!--        <span class="layui-btn layui-btn-primary" data-status="3"><i class="layui-icon layui-icon-reduce-circle"></i> 撤回</span>-->
        <a href="javascript:" onclick="onCancel()" class="layui-btn layui-btn-warm">撤销</a>
        {/if}
        {if ( $detail.check_status eq 4) AND ( $is_create_admin eq 1) }
        <!--        <a class="layui-btn" href="/finance/expense/add?id={$detail.id}">重新编辑</a>-->
        {/if}
    </div>
    {/if}


    <div class="py-3">
        <input type="hidden" name="id" value="{$id}">
        {if !isset($detail.status)||$detail.status==1}
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="onSave">保存</button>
        {/if}
        {if !isset($detail.status)||$detail.status==1||$detail.check_status eq 4}
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">提交审批</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        {/if}
    </div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script type="text/javascript">
    // formSelects.render('selectId');
    formSelects.render('saleId');
</script>
<script>
    const moduleInit = ['tool', 'employeepicker', 'oaTool'];
    let contactList = [];
    let saveList = [];
    let punterId = "{$detail.punterId|default=''}"
    let salesUnitId = "{$detail.salesUnitId|default=''}"
    let dataId="{$detail.id|default=0}"

    function gouguInit() {
        var form = layui.form, tool = layui.tool, level = 0, oaTool = layui.oaTool, laydate = layui.laydate;
        //监听提交
        form.on('submit(webform)', function (data) {

           httpRequest("{:url('add')}", data.field, function (e) {
               layer.msg(e.msg);
               if (e.code == 0) {
                   setTimeout(function () {
                       parent.parent.location.reload();
                   }, 1000);
               }
           },false);
            return false;
        });
        form.on('submit(onSave)', function (data) {

            httpRequest("{:url('save')}", data.field, function (e) {
                layer.msg(e.msg);
                if (e.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            },false);
            return false;
        });
        // laydate.render({
        //     elem: '.mxBjTime',// 指定元素
        //     type: 'date',
        // });

        $("#mxPl").click(function () {
            layer.open({
                type: 1,
                title: '销售合同货期',
                area: ['500px', '200px'],
                btn: ['确定'],
                content: '<form class="layui-form" id="printForm" action="">' +
                    '  <div class="layui-form-item" >' +
                    '    <label class="layui-form-label" style="margin: 0;width: 90px">批量设置货期</label>' +
                    '    <div class="layui-input-block">' +
                    '      <input type="text " style="width: 200px" class="layui-input" id="pl_bjTime"   name="pl_bjTime" title="货期">' +
                    '    </div>' +
                    '  </div>' +
                    '</form>',
                success: function (layero, index) {
                    // 渲染表单

                    form.render();
                    //
                    // // 监听提交事件
                    // form.on('radio(printType)', function(data){
                    //     console.log(data.value); // 获取选中的值
                    //     layer.close(index); // 关闭弹窗
                    // });
                },
                yes: function (index) {
                    var form = document.getElementById('printForm');
                    let times = $("#pl_bjTime").val()
                    $('.tableMany tr').find('.mxBjTime').val(times);
                    layer.close(index);
                }
            });
        })

        form.on('select(punterId)', function (data) {
            console.log('data', data)
            getPunterMember(data.value);
            // getKHXSY(data.value);
        });
        // form.on('select(salesUnitId)', function (data) {
        //    let val=data.value;
        //     getDepartUser(val);
        // });

        form.on('select(contactId)', function (data) {
            let value = data.value;
            if (data.value == '') {
                $("input[name ='contactName']").val();
                $("input[name ='contactPhone']").val();
            } else {
                contactList.forEach(function (item) {
                    if (item.id == value) {
                        $("input[name ='contactName']").val(item.name);
                        $("input[name ='contactPhone']").val(item.phone);
                        return;
                    }
                })
            }
        });
        form.on('select(saveId)', function (data) {
            let value = data.value;
            if (data.value == '') {
                $("input[name ='saveName']").val();
                $("input[name ='savePhone']").val();
                $("input[name ='saveAdd']").val();
            } else {
                saveList.forEach(function (item) {
                    if (item.id == value) {
                        $("input[name ='saveName']").val(item.name);
                        $("input[name ='savePhone']").val(item.phone);
                        $("input[name ='saveAdd']").val(item.address);
                        return;
                    }
                })
            }
        });
        laydate.render({
            elem: "#orderDate"
        })
        // $(document).on('click', '.mxBjTime', function(){
        //     laydate.render({
        //         elem: this // 绑定当前点击的元素
        //     });
        // });

        getPunterMember(punterId);
        // getDepartUser(salesUnitId)

        form.on('select(flowtype)', function (data) {
            var check_type = data.elem[data.elem.selectedIndex].title;
            var formHtml = '<td class="layui-td-gray">审核人<font>*</font></td>\
			<td colspan="5">\
				<input type="text" name="check_admin_name" value="" autocomplete="off" placeholder="请选择审核人" lay-verify="required" lay-reqText="请选择审核人" class="layui-input picker-one"><input type="hidden" name="check_admin_ids" value="">\
			</td>';
            if (check_type == 2) {
                $('#flow_tr').html(formHtml);
                form.render();
            }
            if (data.value == '') {
                return false;
            }
            console.log('checkType', check_type)
            $.ajax({
                url: "/api/index/get_flow_users",
                type: 'get',
                data: {id: data.value},
                success: function (e) {
                    if (e.code == 0) {
                        var flowLi = '';
                        var flow_data = e.data.flow_data;
                        if (e.data.copy_uids && e.data.copy_uids != '') {
                            $('[name="copy_names"]').val(e.data.copy_unames);
                            $('[name="copy_uids"]').val(e.data.copy_uids.split(','));
                        }
                        if (check_type == 1 || check_type == 3) {
                            for (var a = 0; a < flow_data.length; a++) {
                                var userList = '', sign_type = '';
                                if (check_type == 1) {
                                    if (flow_data[a].flow_type == 1) {
                                        userList += '<li style="padding:3px 0">当前部门负责人</li>';
                                    } else if (flow_data[a].flow_type == 2) {
                                        userList += '<li style="padding:3px 0">上级部门负责人</li>';
                                    } else {
                                        if (flow_data[a].flow_type == 3) {
                                            sign_type = ' <span class="layui-badge layui-bg-blue">或签</span>';
                                        }
                                        if (flow_data[a].flow_type == 4) {
                                            sign_type = ' <span class="layui-badge layui-bg-blue">会签</span>';
                                        }
                                        for (var b = 0; b < flow_data[a].user_id_info.length; b++) {
                                            userList += '<li style="padding:3px 0"><img src="' + flow_data[a].user_id_info[b].thumb + '" style="width:24px; height:24px; border-radius:50%; margin-right:8px;" />' + flow_data[a].user_id_info[b].name + '</li>';
                                        }
                                    }
                                } else if (check_type == 3) {
                                    sign_type = ' <span class="layui-badge layui-bg-blue">' + flow_data[a].flow_name + '</span>'
                                    for (var b = 0; b < flow_data[a].user_id_info.length; b++) {
                                        userList += '<li style="padding:3px 0"><img src="' + flow_data[a].user_id_info[b].thumb + '" style="width:24px; height:24px; border-radius:50%; margin-right:8px;" />' + flow_data[a].user_id_info[b].name + '</li>';
                                    }
                                }
                                flowLi += '<li class="layui-timeline-item">\
									<i class="layui-icon layui-timeline-axis">&#xe63f;</i>\
									<div class="layui-timeline-content">\
									  <p class="layui-timeline-title"><strong>第' + (a + 1) + '级审批</strong>' + sign_type + '</p>\
									  <ul>' + userList + '</ul>\
									</div>\
								</li>';
                            }
                            formHtml = '<td class="layui-td-gray">审批流程</td>\
										<td colspan="7">\
											<ul id="flowList" class="layui-timeline">' + flowLi + '</ul>\
										</td>';
                            $('#flow_tr').html(formHtml);
                        }
                    }
                }
            })
        });

        form.on("select(selectProName)", function (data) {

            let proName = '';
            if (data.value > 0 || data.value != '') {
                proName = data.elem.options[data.elem.selectedIndex].text
            }

            $("input[name='glProjectName']").val(proName)
        })
        $('#formBtn').on('click', 'span', function (data) {
            let id = $('[name="id"]').val();
            let content = $('[name="content"]').val();
            let check_status = $(this).data('status');
            let status = 4;
            let flow_type = $('input[name="flow_type"]').val();
            let check_node = 0, check_admin_ids = 0;
            if (flow_type == 0 && check_status == 1) {
                check_node = $('input[name="check_node"]:checked').val();
                check_admin_ids = $('input[name="check_admin_ids"]').val();
                if (!check_node) {
                    layer.msg('请选择下一审批节点');
                    return false;
                }
                if (check_node == 2 && check_admin_ids == '') {
                    layer.msg('请选择下一审批人');
                    return false;
                }
            }

            if (check_status == 1 || check_status == 2) {
                if (content == '') {
                    layer.msg('请输入审批意见');
                    return false;
                }
                let confirmTips = '确定通过该审核？';
                if (check_status == 2) {
                    confirmTips = '确定拒绝该审核？';
                    status = 5;
                }

                layer.confirm(confirmTips, function (index) {
                    $.ajax({
                        url: "/api/index/flow_check",
                        type: 'post',
                        data: {
                            id: id,
                            type: 4,
                            check_node: check_node,
                            check_admin_ids: check_admin_ids,
                            check: check_status,
                            content: content
                        },
                        success: function (e) {
                            layer.msg(e.msg);
                            if (e.code == 0) {
                                parent.parent.location.reload();
                            }
                        }
                    })
                    layer.close(index);
                });
            } else if (check_status == 3) {
                layer.prompt({
                    formType: 2,
                    title: '请输入撤回理由',
                    area: ['500px', '120px'] //自定义文本域宽高
                }, function (value, index, elem) {
                    if (value == '') {
                        layer.msg('请输入撤回理由');
                        return false;
                    }
                    $.ajax({
                        url: "/api/index/flow_check",
                        type: 'post',
                        data: {
                            id: id,
                            type: 4,
                            check: check_status,
                            content: value
                        },
                        success: function (e) {
                            layer.msg(e.msg);
                            if (e.code == 0) {
                                parent.parent.location.reload();
                            }
                        }
                    })
                    layer.close(index);
                });
            }
            return false;
        });
        "{if $id}"
        uploadFilesBuyAny("#fjs", 'sale_contract', '{$id}')
        "{else}"
        uploadFiles("#fjs")
        "{/if}"


        var loadingIndex = '';
        layui.upload.render({
            elem: '#importMx'
            , url: "{:url('api/getSalContractMxImportData')}"
            , accept: 'file'
            , exts: 'xlsx',
            before: function (res) {
                loadingIndex = layer.load(1, {
                    shade: [0.5, '#a09b9b']
                });
            }
            , done: function (res) {
                console.log('res', res)
                layer.msg(res.msg)
                layer.close(loadingIndex)
                if (res.code == 0) {
                    saveAddMx(res.data)
                    $(".blur_class").blur()
                    $(".disAmount").blur()
                }

            },
            error(e) {
                layer.msg('网络异常请稍后再试')
                layer.close(loadingIndex)
            }
        });

        "{if isset($detail.status)&&$detail.status==1}"
            defaultTaxMoney();
        "{/if}"
    }

    let mxIndex = '';

    function saveAddMx(ojb) {
        var lastListItem = $('.tableMany tr:last');
        let aa = $(lastListItem).find(".xuhao")
        if (mxIndex != '') {
            layer.close(mxIndex)
        }

        console.log('aaa', ojb)
        let len = $(aa).text()
        if (!parseInt(len)) {
            len = 0;
        }
        let index = parseInt(len) + 1;
        let str = '';
        for (var i = 0; i < ojb.length; i++) {
            let item = ojb[i];
            let quotationDetailId = item.quotationDetailId || 0;
            let quotationOderNum = item.quotationOderNum || '';
            let bjNum = item.bjNum || '';
            let bjPrice = item.bjPrice || '';
            let bjMoney = item.bjMoney || '';
            let noTaxPrice = item.noTaxPrice || '';
            let bjTime = item.bjTime || '';
            let proCode = item.proCode || '';
            let khlh = item.khlh || '';
            let remark = item.remark || '';
            let disAmount = item.disAmount || '';
            let noTaxMoney = '';
            let taxMoney = 0;
            if (bjMoney) {
                noTaxMoney = noTaxPrice * bjNum;
                noTaxMoney = noTaxMoney.toFixed(4);
                taxMoney = Math.abs(noTaxMoney - bjMoney);
            }
            str += '      <tr>\n' +
                '                        <td>\n' +
                '                            <span class="xuhao">' + index + '</span>\n' +
                '                            <input type="hidden" name="mx[mxid][]" value="">\n' +
                '                            <input type="hidden" name="mx[quotationDetailId][]" value="' + quotationDetailId + '">\n' +
                '                            <input type="hidden" name="mx[mdId][]" value="' + item.id + '">' +
                '<input type="hidden" name="mx[quotationOderNum][]" class="layui-input" value="' + quotationOderNum + '" readonly>\n' +
                '                        </td>\n' +
                // '                        <td>' + item.bar_code + '</td>\n' +
                '                        <td>' + item.brand + '</td>\n' +
                '                        <td>' + item.name + '</td>\n' +
                '                        <td>' + item.specs + '</td>\n' +
                '                        <td>' + item.buyCode + '</td>\n' +
                '                        <td>' + item.unit + '</td>\n' +
                '                        <td><input type="text" name="mx[bjNum][]" value="' + bjNum + '"class="layui-input bjNum blur_class" onblur="getMoneys(this)" lay-verify="required" oninput="digitalFiltering(this,4)"></td>\n' +
                '                        <td><input type="text" name="mx[bjPrice][]" value="' + bjPrice + '" class="layui-input bjPrice blur_class" onblur="getMoneys(this)" lay-verify="required" oninput="digitalFiltering(this,4)"></td>\n' +
                '                        <td> <input type="text" class="layui-input noTaxPrice"value="' + noTaxPrice + '" readonly name="mx[noTaxPrice][]"></td>\n' +
                '                        <td>  <input type="text" class="layui-input bjMoney"value="' + bjMoney + '" readonly name="mx[bjMoney][]"></td>\n' +
                '                        <td>  <input type="text" class="layui-input noTaxMoney" value="' + noTaxMoney + '" readonly name="mx[noTaxMoney][]"></td>\n' +
                '                        <td>  <input type="text" class="layui-input taxMoney" value="' + taxMoney + '" readonly name="mx[taxMoney][]"></td>\n' +
                '                        <td>  <input type="text" class="layui-input disAmount" oninput="digitalFiltering(this,4)"  value="' + disAmount + '"  onblur="countDisCount(this)" name="mx[disAmount][]"></td>\n' +
                '                        <td><input type="text" name="mx[bjTime][]" class="layui-input mxBjTime" value="' + bjTime + '"   placeholder=""></td>\n' +
                '                        <td><input type="text" class="layui-input"  name="mx[khlh][]" value="' + khlh + '" ></td>\n' +
                '                        <td><input type="text" class="layui-input"  name="mx[proCode][]" value="' + proCode + '" ></td>\n' +
                '                        <td><input type="text" class="layui-input" name="mx[remark][]" value="' + remark + '"></td>\n' +
                // '                        <td></td>\n' +
                '                        <td><a href="javascript:" class="layui-btn layui-btn-xs layui-btn-danger" onclick="delMx(\'\',this)">删除</a></td>\n' +
                '                    </tr>';
            index++;
        }
        $("#mxTbdy").append(str)
        layui.form.render();
        getCounts();
        defaultTaxMoney();
    }


    function delMx(id = '', e) {
        if (id == '') {
            $(e).parent().parent().remove()
            getCounts();
            return;
        }
        $.ajax({
            url: "{:url('removeMx')}",
            type: 'post',
            dataType: 'json',
            data: {id: id},
            success: function (res) {
                if (res.code != 0) {
                    layer.msg(res.msg)
                } else {
                    $(e).parent().parent().remove()
                    getCounts();
                }
            },
            error(e) {
                layer.msg('网络错误，请稍后')
            }
        })
    }


    function getMoneys(ojb) {
        let tax = $("#tax").val()
        if (!parseInt(tax)) {
            tax = 0;
        } else {
            tax = parseFloat(tax / 100)
        }
        let trObj = $(ojb).parent().parent();
        let bjNum = trObj.find(".bjNum").val()
        let bjPrice = trObj.find(".bjPrice").val()

        let noTaxPrice = 0;
        let noTaxMoney = 0;
        let taxMoney = 0;
        let bjMoney = 0;
        noTaxPrice = parseFloat(bjPrice / (1 + tax))
        bjMoney = parseFloat(bjNum * bjPrice)
        noTaxMoney = parseFloat(bjNum * noTaxPrice);
        taxMoney = parseFloat(Math.abs(bjMoney - noTaxMoney))
        trObj.find(".noTaxPrice").val(noTaxPrice.toFixed(4))
        trObj.find(".bjMoney").val(bjMoney.toFixed(4))
        trObj.find(".noTaxMoney").val(noTaxMoney.toFixed(4))
        trObj.find(".taxMoney").val(taxMoney.toFixed(4))
        getCounts();
    }

    function getPunterMember(punterId = '') {
        $("#contactId").empty();
        $("#saveId").empty();
        $("input[name ='contactName']").val();
        $("input[name ='contactPhone']").val();
        $("input[name ='saveName']").val();
        $("input[name ='savePhone']").val();
        $("input[name ='saveAdd']").val();
        contactList = [];
        saveList = [];
        if (punterId == '') {
            layui.form.render();
            return;
        }
        httpRequest("{:url('api/getPurveyorMember')}", {purveyorId: punterId}, function (res) {
            if (res.code == 0) {
                contactList = res.data.contactList;
                saveList = res.data.saverList;
                let contactStr = '  <option value="">请选择</option>';
                let saverStr = '  <option value="">请选择</option>';
                let contactId = "{$detail.contactId|default='0'}"
                let saveId = "{$detail.saveId|default='0'}"
                contactList.forEach(function (item) {
                    let cStr = '';
                    if (item.id == contactId) {
                        cStr = 'selected';
                    }
                    contactStr += '  <option value="' + item.id + '" ' + cStr + '>' + item.name + '</option>'
                })
                saveList.forEach(function (item) {
                    let s_Str = '';
                    if (item.id == saveId) {
                        s_Str = 'selected';
                    }
                    saverStr += '  <option value="' + item.id + '" ' + s_Str + '>' + item.name + '</option>'
                })
                console.log(saverStr)
                $("#contactId").append(contactStr);
                $("#saveId").append(saverStr);
                layui.form.render();
            } else {

            }
        }, false)
    }

    function getDepartUser(departId = '') {
        $("#saleId").empty();
        if (departId == '') {
            layui.form.render();
            return;
        }
        httpRequest("{:url('api/getDepartUser')}", {did: departId}, function (res) {
            if (res.code == 0) {
                let data = res.data;
                let saleStr = '';
                let saleId = "{$detail.saleId|default='0'}"
                data.forEach(function (item) {
                    let sstr = '';
                    if (item.id == saleId) {
                        sstr = 'selected';
                    }
                    saleStr += ' <option value="' + item.id + '" ' + sstr + '>' + item.name + '</option>';
                })

                $("#saleId").append(saleStr);
                layui.form.render();
            }
        }, false)
    }

    function getKHXSY(punterId = '') {
        $("#saleId").empty();
        if (punterId == '') {
            layui.form.render();
            formSelects.render('saleId');
            return;
        }
        httpRequest("{:url('api/getKHXSY')}", {purveyorId: punterId}, function (res) {
            if (res.code == 0) {
                let data = res.data;
                // let str='  <option value="">请选择</option>';
                let str = '';
                let saleIds = "{$detail.saleId|default=0}"
                let saleidArr = saleIds.split(",");
                data.forEach(function (item) {
                    let cStr = '';
                    if (saleidArr.includes('' + item.id + '')) {
                        cStr = 'selected';
                    }
                    str += '  <option value="' + item.id + '" ' + cStr + '>' + item.name + '</option>'
                })
                $("#saleId").append(str);
                layui.form.render();
                formSelects.render('saleId');
            } else {

            }
        }, false)

    }

    function getBjd() {
        let khId = $("#punterId").val();
        let quoDetialIs = [];
        $("input[name='mx[quotationDetailId][]']").each(function () {
            let val = $(this).val();
            quoDetialIs.push(val)
        });

        if (khId == '') {
            layer.msg('请选择客户');
            return
        }
        boxModel("{:url('quoOrder')}?khId=" + khId + '&detailIds=' + quoDetialIs.join(','), '关联报价单', '1000')
    }

    function onSure() {
        httpRequest("{:url('onSure')}", {id: "{$id}"}, function (res) {
            if (res.code == 0) {
                parent.parent.location.reload();
            }
        })
    }

    function onSub() {
        httpRequest("{:url('onSub')}", {id: "{$id}"}, function (res) {
            if (res.code == 0) {
                parent.parent.location.reload();
            }
        })
    }

    function onChick(type = 1) {
        httpRequest("{:url('onChick')}", {id: "{$id}", type: type}, function (res) {
            if (res.code == 0) {
                parent.parent.location.reload();
            }
        })
    }

    function getCounts() {
        let nums = 0;
        let moneys = 0;
        $('.bjNum').each(function () {
            let thisNum = $(this).val();
            if (thisNum == '') {
                thisNum = 0;
            }
            thisNum = parseFloat(thisNum)
            nums += thisNum;
        })
        $('.bjMoney').each(function () {
            let thisMoney = $(this).val();
            if (thisMoney == '') {
                thisMoney = 0;
            }
            thisMoney = parseFloat(thisMoney)
            moneys += thisMoney;
        })

        $('#numCount').text(nums)
        $('#priceCount').text(moneys)
    }

    function onCancel() {
        httpRequest("{:url('onCancel')}", {id: "{$id}"}, function (res) {
            if (res.code == 0) {
                parent.parent.location.reload();
            }
        })
    }

    function addPro() {
        let punterId = $("#punterId").val();
        layui.tool.box("{:url('addMx')}?punterId=" + punterId, '选择产品', '1000', '600')

    }

    function countDisCount(obj) {
        let coutAmount = 0;

        let trObj = $(obj).parent().parent();
        let bjMoney=trObj.find(".bjMoney").val()
        let thisdis=$(obj).val();
        if (parseFloat(bjMoney)<parseFloat(thisdis)){
            layer.msg('优惠金额填写有误');
            $(obj).val('');
            return;
        }
        $('.disAmount').each(function () {
            let thisNum = $(this).val();
            if (thisNum == '') {
                thisNum = 0;
            }
            thisNum = parseFloat(thisNum)
            coutAmount += thisNum;
        })
        $('.countDiscount').val(coutAmount)
    }
    function defaultTaxMoney() {
        $('.bjNum').blur()
    }
</script>
{/block}
<!-- /脚本 -->